package com.song.eduservice.controller;


import com.song.commonutils.ResultObj;
import com.song.eduservice.client.vodfeign.VodFeign;
import com.song.eduservice.entity.EduVideo;
import com.song.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-03-29
 */
@Api(description = "小节管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodFeign vodFeign;

    /**
     * 添加小节
     * @param eduVideo
     * @return
     */
    @ApiOperation(value = "添加小节")
    @PostMapping("/")
    public ResultObj addVideo(@RequestBody EduVideo eduVideo){
        boolean save = eduVideoService.save(eduVideo);
        if(save){
           return ResultObj.ADD_SUCCESS;
        }
        return ResultObj.ADD_ERROR;
    }

    /**
     * 删除小节
     * @param id
     * @return
     */
    @ApiOperation(value = "删除小节与视频")
    @DeleteMapping("/{id}")
    public ResultObj deleteVideo(@PathVariable String id){

        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(videoSourceId!=null){
            //删除视频
            vodFeign.deleteAlyVideo(videoSourceId);
            //删除小节
            eduVideoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }

    /**
     * 修改小节
     * @param eduVideo
     * @return
     */
    @ApiOperation(value = "修改小节")
    @PutMapping("/")
    public ResultObj updateVideo(@RequestBody EduVideo eduVideo){
        boolean save = eduVideoService.updateById(eduVideo);
        if(save){
            return ResultObj.UPDATE_SUCCESS;
        }
        return ResultObj.UPDATE_ERROR;
    }

    /**
     * 按照id查询小节
     * @param id
     * @return
     */
    @ApiOperation(value = "按照id查询小节")
    @GetMapping("/{id}")
    public ResultObj findVideo(@PathVariable String id){
        EduVideo byId = eduVideoService.getById(id);
        return ResultObj.ok().data("data",byId);
    }

    /**
     * 上传视频
     * @param file
     * @return
     */

    @ApiOperation(value = "上传视频")
    @PostMapping("/uploadAlyiVideo")
    public String uploadAlyiVideo(MultipartFile file){
        String videoId =vodFeign.uploadAlyiVideo(file);
        return videoId;
    }
    /**
     * 删除阿里云视频
     * @param videoSourceId
     * @return
     */
    @ApiOperation(value = "删除阿里云视频")
    @DeleteMapping("/aliyun/{videoSourceId}")
    public ResultObj deleteAlyVideo(@PathVariable String videoSourceId){
        if(videoSourceId!=null){
            vodFeign.deleteAlyVideo(videoSourceId);
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }

}

